<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'buy';
$return=array();
//购物送券
$tabs=array(
			array('op'=>'huodong','title'=>'活动送券'),
			array('op'=>'buy','title'=>'购物送券'),
			array('op'=>'share','title'=>'分享送券'),
			array('op'=>'new','title'=>'新人发券'),
			array('op'=>'enough','title'=>'满额送券'),			
			array('op'=>'coupon','title'=>'用券送券'),
);
if($_RQ['op']=='huodong'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_coupon_huodongsendtask',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['title']=pdo_getcolumn('jy_weishop_coupon',array('id'=>$v['couponid']),'title');
	}
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'del0','edit_op'=>'addhuodong','list'=>array(
			array('name'=>'title','title'=>'优惠券名称','type'=>'string'),
			array('name'=>'sendnum','title'=>'每人发送数量','type'=>'string'),
			array('name'=>'num','title'=>'剩余','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addhuodong'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='addhuodong'){
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_weishop_coupon_huodongsendtask',array('id'=>$id));
		$item['limittime']=json_decode($item['limittime'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['sendpoint']=array('1'=>'(推荐)订单完成后（包括子订单内所有订单收货后）发送优惠券','2'=>'订单付款后发送优惠券');
	$return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$htmls=array(
		'type'=>'edit',
		'post'=>'post0',
		'desc'=>'分类设置',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'select','name'=>'couponid','title'=>'选择优惠券','options'=>'coupon'),
			array('name'=>'sendnum','title'=>'每人发送数量','type'=>'string','help'=>'默认每人发送数量为1，不能大于10'),
			array('name'=>'num','title'=>'剩余数量','type'=>'string','help'=>'当剩余数量小于每人发送数量,发送任务停止'),
			array('name'=>'limittime','title'=>'限时设置','type'=>'time_range'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','help'=>'开启后,首页会弹窗'),
			)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post0'){
	$postdata=get_postdata();
	$data=array_fields(array('goodsid','couponid','sendnum','limittime','num','status'),$postdata);
	$data['limittime']=json_encode($data['limittime']);
	if($data['sendnum']>10){
		exi('每位用户发送数量不能大于10！','error');
	}
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_coupon_huodongsendtask',$data,array('id'=>$id));
				exi('设置成功！','','huodong');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_coupon_huodongsendtask',$data);
				exi('添加成功！','','huodong');
			}
}
if($_RQ['op']=='sendtask0'){
	$id=intval($_RQ['id']);
	$params=array(
		'mid'=>$this->order['mid'],
		'ac_type'=>'goods',
		'ac_id'=>$coupon['id'],
		'remark'=>'活动送券',
	);
	$this->model('coupon')->give_coupon($params);
	exi('发放成功！请勿多次发放！');
}
if($_RQ['op']=='del0'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon_goodsendtask',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon_goodsendtask',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='buy'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	
	$return['list']=pdo_getslice('jy_weishop_coupon_goodsendtask',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['title']=pdo_getcolumn('jy_weishop_coupon',array('id'=>$v['couponid']),'title');
	}
	 
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'del1','edit_op'=>'addbuy','list'=>array(
			array('name'=>'title','title'=>'优惠券名称','type'=>'string'),
			array('name'=>'sendnum','title'=>'每人发送数量','type'=>'string'),
			array('name'=>'num','title'=>'剩余','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addbuy'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='addbuy'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_coupon_goodsendtask',array('id'=>$id));
	$item['limittime']=json_decode($item['limittime'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['sendpoint']=array('1'=>'(推荐)订单完成后（包括子订单内所有订单收货后）发送优惠券','2'=>'订单付款后发送优惠券');
	$return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['goods']=pdo_getall('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id','title'));
	$htmls=array(
		'type'=>'edit',
		'post'=>'post1',
		'desc'=>'分类设置',
		'tabs'=>$tabs,
		'edit'=>array(
			array('type'=>'select','name'=>'goodsid','title'=>'选择商品','options'=>'goods','help'=>'购物送券，必须选择商品，防止大意送券！'),
			array('type'=>'select','name'=>'couponid','title'=>'选择优惠券','options'=>'coupon'),
			array('name'=>'sendnum','title'=>'每人发送数量','type'=>'string','help'=>'默认每人发送数量为1，不能大于10'),
			array('name'=>'num','title'=>'剩余数量','type'=>'string','help'=>'当剩余数量小于每人发送数量,发送任务停止'),
			array('name'=>'limittime','title'=>'限时设置','type'=>'time_range'),
			array('name'=>'sendpoint','title'=>'发送节点','type'=>'radio','options'=>'sendpoint'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status','help'=>'开启后,首页会弹窗'),
			)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post1'){
	$postdata=get_postdata();
	$data=array_fields(array('goodsid','couponid','sendnum','num','limittime','sendpoint','status'),$postdata);
	$data['limittime']=json_encode($data['limittime']);
	if(!$data['goodsid']){
		exi('购物送券，必须选择商品，防止大意送券！','error');
	}
	if($data['sendnum']>10){
		exi('每位用户发送数量不能大于10！','error');
	}
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_coupon_goodsendtask',$data,array('id'=>$id));
				exi('设置成功！','','buy');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_coupon_goodsendtask',$data);
				exi('添加成功！','','buy');
			}
}
if($_RQ['op']=='del1'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon_goodsendtask',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon_goodsendtask',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}

//分享送券

if($_RQ['op']=='share'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	
	$return['list']=pdo_getslice('jy_weishop_coupon_sendticket_share',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	
	 
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'del2','edit_op'=>'addshare','list'=>array(
			
			array('name'=>'sharetitle','title'=>'分享标题','type'=>'string'),
			array('name'=>'shareicon','title'=>'分享图片','type'=>'image'),
			
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addshare'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='addshare'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_coupon_sendticket_share',array('id'=>$id));
	
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['sendpoint']=array('1'=>'(推荐)订单完成后（包括子订单内所有订单收货后）发送优惠券','2'=>'订单付款后发送优惠券');
	$coupon=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['coupon']=array();
	foreach($coupon as $v){
		$id=$v['id'];
		$return['coupon'][$id]=$v['title'];
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post2',
		'desc'=>'分类设置',
		'tabs'=>$tabs,
		'edit'=>array(
			
			array('type'=>'checkbox','name'=>'couponid','title'=>'选择优惠券','options'=>'coupon'),
			array('name'=>'sharetitle','title'=>'分享标题','type'=>'string'),
			array('name'=>'shareicon','title'=>'分享图片','type'=>'image'),
			array('name'=>'sharedesc','title'=>'分享描述','type'=>'string'),
			array('name'=>'price','title'=>'发券条件','type'=>'string','help'=>'单笔订单满足此金额发送，0为任意订单可发'),
			array('name'=>'paixu','title'=>'排序','type'=>'string','help'=>'数字越大越靠前'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			)
			);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post2'){
	$postdata=get_postdata();
	$data=array_fields(array('sharetitle','couponid','shareicon','paixu','sharedesc','price','status'),$postdata);
	
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_coupon_sendticket_share',$data,array('id'=>$id));
				exi('设置成功！','','share');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_coupon_sendticket_share',$data);
				exi('添加成功！','','share');
			}
}
if($_RQ['op']=='del2'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon_sendticket_share',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon_sendticket_share',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}

//新人送券

if($_RQ['op']=='new'){
	
	$item=pdo_get('jy_weishop_coupon_sendticket',array('uniacid'=>$_SESSION['uniacid']));
	$item['limittime']=json_decode($item['limittime'],true);
    $return['item']=$item;
	
	
	$coupon=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['coupon']=array();
	foreach($coupon as $v){
		$id=$v['id'];
		$return['coupon'][$id]=$v['title'];
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post3',
		'desc'=>'',
		'tabs'=>$tabs,
		'edit'=>array(
			array('name'=>'status','title'=>'是否开启','type'=>'radio','options'=>'status','help'=>'开启后,首页会弹窗'),
			array('type'=>'checkbox','name'=>'couponid','title'=>'选择优惠券','options'=>'coupon'),
			array('type'=>'radio','name'=>'intime','title'=>'设置活动期限','options'=>'status'),
			array('name'=>'limittime','title'=>'活动期限','type'=>'time_range','show'=>array('intime'=>1)),
		)
	);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	
	$return['htmls']=$htmls;
	exi($return);
	}
if($_RQ['op']=='post3'){
	$postdata=get_postdata();
	$data=array_fields(array('status','couponid','limittime','intime'),$postdata);
	$data['limittime']=json_encode($data['limittime']);
	$item=pdo_get('jy_weishop_coupon_sendticket',array('uniacid'=>$_SESSION['uniacid']));
			if($item){
				pdo_update('jy_weishop_coupon_sendticket',$data,array('uniacid'=>$_SESSION['uniacid']));
				exi('设置成功！','','new');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				 $data['createtime']=TIMESTAMP;
				pdo_insert('jy_weishop_coupon_sendticket',$data);
				exi('设置成功！','','new');
			}
}
//满额送券

if($_RQ['op']=='enough'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	
	$return['list']=pdo_getslice('jy_weishop_coupon_sendtasks',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['title']=pdo_getcolumn('jy_weishop_coupon',array('id'=>$v['couponid']),'title');
	}
	 
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'del4','edit_op'=>'addenough','list'=>array(
			array('name'=>'price','title'=>'满足价格','type'=>'string'),
			array('name'=>'num','title'=>'每人发送数量','type'=>'string'),
			array('name'=>'num','title'=>'剩余','type'=>'string'),
			
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addenough'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='addenough'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_coupon_sendtasks',array('id'=>$id));
	$item['limittime']=json_decode($item['limittime'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['sendpoint']=array('1'=>'(推荐)订单完成后（包括子订单内所有订单收货后）发送优惠券','2'=>'订单付款后发送优惠券');
	$coupon=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	$return['coupon']=array();
	foreach($coupon as $v){
		$id=$v['id'];
		$return['coupon'][$id]=$v['title'];
	}
	$htmls=array(
		'type'=>'edit',
		'post'=>'post4',
		'desc'=>'分类设置',
		'tabs'=>$tabs,
		'edit'=>array(
			array('name'=>'price','title'=>'满足价格','type'=>'string','help'=>'当订单金额达到此金额要求时,触发发送优惠券任务'),
			array('type'=>'checkbox','name'=>'couponid','title'=>'选择优惠券','options'=>'coupon'),
			array('name'=>'sendnum','title'=>'每人发送数量','type'=>'string','help'=>'默认每人发送数量为1，不能大于10'),
			array('name'=>'num','title'=>'剩余数量','type'=>'string','help'=>'当剩余数量小于每人发送数量,发送任务停止'),
			array('name'=>'limittime','title'=>'限时设置','type'=>'time_range'),
			array('name'=>'sendpoint','title'=>'发送节点','type'=>'radio','options'=>'sendpoint'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			)
			);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post4'){
	$postdata=get_postdata();
	$data=array_fields(array('limittime','couponid','price','sendnum','num','status','sendpoint'),$postdata);
	$data['limittime']=json_encode($data['limittime']);
	if($data['sendnum']>10){
		exi('每位用户发送数量不能大于10！','error');
	}
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_coupon_sendtasks',$data,array('id'=>$id));
				exi('设置成功！','','enough');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_coupon_sendtasks',$data);
				exi('添加成功！','','enough');
			}
}
if($_RQ['op']=='del4'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon_sendtasks',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon_sendtasks',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}

//用券送券

if($_RQ['op']=='coupon'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
    $orderby='id desc';
	$total='';
	
	$return['list']=pdo_getslice('jy_weishop_coupon_usesendtasks',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['title']=pdo_getcolumn('jy_weishop_coupon',array('id'=>$v['usecouponid']),'title');
		$v['title2']=pdo_getcolumn('jy_weishop_coupon',array('id'=>$v['couponid']),'title');
	}
	 
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','del_op'=>'del5','edit_op'=>'addcoupon','list'=>array(
			array('name'=>'title','title'=>'使用的优惠券名称','type'=>'string'),
			array('name'=>'title2','title'=>'赠送的优惠券名称','type'=>'string'),
			array('name'=>'num','title'=>'剩余数量','type'=>'string'),	
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
		'search_one'=>array('name'=>'title','add_op'=>'addcoupon'),
	);
	$return['htmls']=$htmls;
	
	exi($return);
}
if($_RQ['op']=='addcoupon'){
	$id=intval($_RQ['id']);
	if($id){
	$item=pdo_get('jy_weishop_coupon_usesendtasks',array('id'=>$id));
	$item['limittime']=json_decode($item['limittime'],true);
	}else{
		$item=array();
	}
    $return['item']=$item;
	$return['status']=array('0'=>'关闭','1'=>'开启');
	$return['sendpoint']=array('1'=>'(推荐)订单完成后（包括子订单内所有订单收货后）发送优惠券','2'=>'订单付款后发送优惠券');
	$return['coupon']=pdo_getall('jy_weishop_coupon',array('uniacid'=>$_SESSION['uniacid']),array('id','title'));
	
	$htmls=array(
		'type'=>'edit',
		'post'=>'post5',
		'desc'=>'分类设置',
		'tabs'=>$tabs,
		'edit'=>array(
			array('name'=>'usecouponid','title'=>'选择使用的优惠券','options'=>'coupon','type'=>'select'),
			array('type'=>'select','name'=>'couponid','title'=>'选择赠送的优惠券','options'=>'coupon'),
			array('name'=>'sendnum','title'=>'每人发送数量','type'=>'string','help'=>'默认每人发送数量为1，不能大于10'),
			array('name'=>'num','title'=>'剩余数量','type'=>'string','help'=>'当剩余数量小于每人发送数量,发送任务停止'),
			array('name'=>'limittime','title'=>'限时设置','type'=>'time_range'),
			
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			)
			);
	$return['status']=array('0'=>'关闭','1'=>'开启');
	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post5'){
	$postdata=get_postdata();
	$data=array_fields(array('limittime','couponid','usecouponid','sendnum','num','status'),$postdata);
	$data['limittime']=json_encode($data['limittime']);
	$id=intval($postdata['id']);
			if($id){
				pdo_update('jy_weishop_coupon_usesendtasks',$data,array('id'=>$id));
				exi('设置成功！','','coupon');
			}else{
			    $data['uniacid']=$_SESSION['uniacid'];
				pdo_insert('jy_weishop_coupon_usesendtasks',$data);
				exi('添加成功！','','coupon');
			}
}
if($_RQ['op']=='del5'){

	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishop_coupon_usesendtasks',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishop_coupon_usesendtasks',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
?>